﻿using CQIE.JonJack.SplLibraryService._IBooksService;
using CQIE.JonJack.SplLibraryService._IPublicService;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System.Security.Claims;

namespace CQIE.JonJack.SplLibraryManagementSystem.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class BooksController : ControllerBase
    {
        private readonly IBooksService bookService;


        public BooksController(IBooksService bookService)
        {
            this.bookService = bookService;
        }

       
        [HttpPost]
        public ActionResult destructBook(int bid)
        {

            return Ok(bookService.destructBook(bid));
        }

        public record NBook(string newBook);
        [HttpPost]
        public ActionResult setNewBook([FromBody]NBook result)
        {
            bookService.setNewBook(result.newBook);
            return Ok();
        }

        public record BBook(string bookId,string endTime);
        [HttpPost]
        [Authorize]
        public ActionResult BorrowBook([FromBody] BBook bbook)
        {
            //获取当前登录用户
            string stuNum = this.User.FindFirst(ClaimTypes.NameIdentifier)!.Value;
            return Ok(bookService.BorrowBook(bbook.bookId,bbook.endTime,stuNum));
        }

        [HttpGet]
        [Authorize]
        public ActionResult getAllBorrowedBook()
        {
            //获取当前登录用户
            string stuNum = this.User.FindFirst(ClaimTypes.NameIdentifier)!.Value;
            return Ok(bookService.getAllBorrowedBook(stuNum));
        }

        [HttpGet]
        [Authorize]
        public ActionResult getHistoryBook()
        {
            //获取当前登录用户
            string stuNum = this.User.FindFirst(ClaimTypes.NameIdentifier)!.Value;
            return Ok(bookService.getHistoryBook(stuNum));
        }
        
        public record BookR(string bookId);
        [HttpPost]
        [Authorize]
        public ActionResult ReturnBook([FromBody]BookR bookR)
        {
            //获取当前登录用户
            string stuNum = this.User.FindFirst(ClaimTypes.NameIdentifier)!.Value;
            return Ok(bookService.ReturnBook(bookR.bookId, stuNum));
        }

        [HttpGet]
        [Authorize]
        public ActionResult StatisticsBook()
        {
            return Ok(bookService.StatisticsBook());
        }
    }
}
